﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace Publicador
{
    public partial class Transmision : Form
    {
        private Process P = null;
        public bool Activo = false;
        public string MUNICIPIO = null;
        public string USUARIO = null;
        E14 e14 = null;

        public Transmision()
        {
            InitializeComponent();
            e14 = new E14(ref txtPublicados, ref txtNovedades, ref toolStripStatusLabel1, ref groupBox2);
            try
            {
                if (!File.Exists(@"Config.cnf"))
                {
                    string filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\PegasusImaging.ActiveX.ImagXpress9.dll";
                    //'/s' : Specifies regsvr32 to run silently and to not display any message boxes.
                    string arg_fileinfo = "/s" + " " + "\"" + filePath + "\"";
                    Process reg = new Process();
                    //This file registers .dll files as command components in the registry.
                    reg.StartInfo.FileName = "regsvr32.exe";
                    reg.StartInfo.Arguments = arg_fileinfo;
                    reg.StartInfo.UseShellExecute = false;
                    reg.StartInfo.CreateNoWindow = true;
                    reg.StartInfo.RedirectStandardOutput = true;
                    reg.Start();
                    reg.WaitForExit();
                    reg.Close();
                    if (!Directory.Exists(@"C:\DIGITALIZACION"))
                        Directory.CreateDirectory(@"C:\DIGITALIZACION");
                    if (!Directory.Exists(@"C:\DIGITALIZACION\SCANNER"))
                        Directory.CreateDirectory(@"C:\DIGITALIZACION\SCANNER");
                    if (!Directory.Exists(@"C:\DIGITALIZACION\AZURE"))
                        Directory.CreateDirectory(@"C:\DIGITALIZACION\AZURE");
                    File.Create(@"Config.cnf");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
            try
            {
                P = Process.Start(@"TransmisionAzure.exe");
            }
            catch (Exception)
            { }
        }

        private void Transmision_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                P.Kill();
            }
            catch (Exception)
            { }
            System.Environment.Exit(0);
        }

        private void Transmision_Shown(object sender, EventArgs e)
        {
            this.Text = "Transmisión E14: " + MUNICIPIO;
            this.labMunicipio.Text = "MUNICIPIO: " + MUNICIPIO;
            comboBox1.DataSource = e14.ObtenerPuestos(MUNICIPIO);
            butRefrescar_Click(null, null);            
        }

        private void limpiarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtNovedades.Text = string.Empty;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Ping ping = new Ping();
            try
            {
                PingReply pingReply = ping.Send(Properties.Settings.Default.E14IpPing);
                if (pingReply.Status == IPStatus.Success)
                {
                    Activo = true;
                    e14.Activo = true;
                }
                else
                {
                    Activo = false;
                    e14.Activo = false;
                }
            }
            catch (Exception)
            {
                Activo = false;
            }
            Validar();
        }

        private void butTransmitir_Click(object sender, EventArgs e)
        {
            e14.Iniciar(MUNICIPIO, USUARIO);
            //butRefrescar_Click(null, null);
        }

        public void Validar()
        {
            if (this.Activo)
            {
                picConnOff.Visible = false;
                picConnOn.Visible = true;
                txtConnEstado.Text = "Conectado";
                butTransmitir.Enabled = true;
                /*
                if (!timTransmitir.Enabled)
                {
                    timTransmitir.Enabled = true;
                    timTransmitir.Interval = 36000;
                }
                */                
            }
            else
            {
                picConnOn.Visible = false;
                picConnOff.Visible = true;
                txtConnEstado.Text = "No Conectado";
                //butTransmitir.Enabled = false;
                //timTransmitir.Enabled = false;
                //timTransmitir.Interval = 3000;
            }
        }

        private void timTransmitir_Tick(object sender, EventArgs e)
        {
            if (chkAutomatico.Checked)
            {
                butTransmitir_Click(null, null);
            }
        }

        private void butRefrescar_Click(object sender, EventArgs e)
        {
            DataRowView row = (DataRowView)comboBox1.SelectedItem;
            dataGridView1.DataSource = e14.ObtenerPendientes(MUNICIPIO, row[0].ToString().Trim());
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            butRefrescar_Click(null, null);
        }

        private void pictureBox1_DoubleClick_1(object sender, EventArgs e)
        {
            try
            {
                Process.Start(@"TeamViewer_Setup_es.exe");
            }
            catch (Exception)
            {
            }   
        }

    }
}
